package com.upc.feedish;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.upc.feedish.utils.DataBaseController;
import com.upc.feedish.utils.MyTools;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;

public class UsermodifyActivity extends AppCompatActivity {
    ImageView imageView1;
    ImageView imageView2;
    SharedPreferences sp;
    String token;
    DataBaseController dataBaseController;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_usermodify);

        dataBaseController=new DataBaseController();
        sp = getSharedPreferences("data",MODE_PRIVATE);
        token=sp.getString("token","");
        imageView1 = findViewById(R.id.modify_camera);
        imageView2 = findViewById(R.id.modify_album);
        imageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 关键：新建相机的 Intent
                startActivityForResult(intent, 1); // 加载相机 Activity
            }
        });
        imageView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开本地相册
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                //设定结果返回
                startActivityForResult(i, 2);
            }
        });
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Bitmap bm = null;
        try {
            //自己拍摄
            if (requestCode == 1 && resultCode == RESULT_OK) {
                bm = (Bitmap) data.getExtras().get("data");
                ByteArrayOutputStream os=new ByteArrayOutputStream();
                bm.compress(Bitmap.CompressFormat.JPEG,100,os);
                byte[] re = os.toByteArray();
                os.flush();
                os.close();
                String name=sp.getString("name","");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        dataBaseController.updateFile(token,re,name);
                    }
                }).start();

                Intent intent=new Intent();
                setResult(100,intent);
                finish();
            }
            //从本地相册选择
            else if (requestCode == 2 && resultCode == RESULT_OK) {
                Uri selectedImage = data.getData();
                bm = BitmapFactory.decodeStream(this.getContentResolver().openInputStream(selectedImage));
                ByteArrayOutputStream os=new ByteArrayOutputStream();
                bm.compress(Bitmap.CompressFormat.JPEG,100,os);
                byte[] re = os.toByteArray();
                os.flush();
                os.close();
                String name=sp.getString("username","");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        dataBaseController.updateFile(token,re,name);
                    }
                }).start();
                savePic(re,name);
                Intent intent=new Intent();
//                intent.putExtra("pic",re);
                setResult(100,intent);
                finish();
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void savePic(byte[] bytes,String name) {
        String filename = name + ".jpg";
        try (FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE)) {
            fos.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}